package service.impl;

import utils.MyBatisUtils;
import dao.ComplaintUpgradeMapper;
import entity.ComplaintUpgrade;
import enum_var.ComplaintStatus;
import service.ComplaintService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 投诉服务实现类
 */
public class ComplaintServiceImpl implements ComplaintService {

    @Override
    public void createComplaint(ComplaintUpgrade complaint) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            mapper.insert(complaint);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("创建投诉失败: " + e.getMessage());
        }
    }

    @Override
    public ComplaintUpgrade getComplaintById(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            return mapper.selectById(id);
        }
    }

    @Override
    public List<ComplaintUpgrade> getComplaintsByUserId(Long userId) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            return mapper.selectByUserId(userId);
        }
    }

    @Override
    public List<ComplaintUpgrade> getComplaintsByStatus(ComplaintStatus status) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            return mapper.selectByStatus(status);
        }
    }

    @Override
    public List<ComplaintUpgrade> getAllComplaints() {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession()) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            return mapper.selectAll();
        }
    }

    @Override
    public void updateComplaint(ComplaintUpgrade complaint) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            mapper.updateById(complaint);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("更新投诉失败: " + e.getMessage());
        }
    }

    @Override
    public void deleteComplaint(Long id) {
        try (SqlSession sqlSession = MyBatisUtils.getSqlSession(false)) {
            ComplaintUpgradeMapper mapper = sqlSession.getMapper(ComplaintUpgradeMapper.class);
            mapper.deleteById(id);
            sqlSession.commit();
        } catch (Exception e) {
            throw new RuntimeException("删除投诉失败: " + e.getMessage());
        }
    }
}
